/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.mlc.swing.layout.LayoutConstraintsManager;

/**
 * AddMarksPopup.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class AddMarksPopup extends JFrame implements ActionListener {

    private static final String ADD_MARK_PANEL_LAYOUT = "ADD_MARK_PANEL_LAYOUT.xml";

    private JPanel addMarksPanel;

    // here are declarations for the controls you created
    private JLabel timeLabel;
    private JTextField timeTextField;
    private JLabel markLabel;
    private JTextField markTextField;
    private JLabel selectMarkNumberLabel;
    private JComboBox markNumberComboBox;
    private JButton doneButton;
    private JButton cancelButton;
    private JButton addButton;

    public AddMarksPopup() {
	super("Add Marks");
	initComponents();
	addComponents();
	setSize(320, 180);
	this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * 
     */
    private void addComponents() {

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(ADD_MARK_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	addMarksPanel.setLayout(panelLayout);

	// here we add the controls to the container.
	addMarksPanel.add(timeLabel, "timeLabel");
	addMarksPanel.add(timeTextField, "timeTextField");
	addMarksPanel.add(markLabel, "markLabel");
	addMarksPanel.add(markTextField, "markTextField");
	addMarksPanel.add(selectMarkNumberLabel, "selectMarkNumberLabel");
	addMarksPanel.add(markNumberComboBox, "markNumberComboBox");
	addMarksPanel.add(doneButton, "doneButton");
	addMarksPanel.add(cancelButton, "cancelButton");
	addMarksPanel.add(addButton, "addButton");

	this.add(addMarksPanel);

    }

    /**
     * 
     */
    private void initComponents() {

	// here are declarations for the controls you created
	addMarksPanel = new JPanel();
	timeLabel = new JLabel("Time");
	timeTextField = new JTextField();
	markLabel = new JLabel("Mark");
	markTextField = new JTextField();
	selectMarkNumberLabel = new JLabel("Mark Number");
	markNumberComboBox = new JComboBox();
	doneButton = new JButton("Done");
	cancelButton = new JButton("Cancel");
	addButton = new JButton("Add");
    }

    public void showMarkPanel() {
	this.add(addMarksPanel);
	this.setVisible(true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
	Object source = e.getSource();
	if (source.equals(doneButton)) {
	    // check if all the mark values are entered
	    this.dispose();
	} else if (source.equals(cancelButton)) {
	    // confirm user wants to cancel, tell him that he cannot proceed until all the data is entered
	    this.dispose();
	}
    }

    /**
     * @param b
     * @param numberOfMarks
     */
    public void setVisible(boolean b, String numberOfMarks) {
	int totalMarks = Integer.valueOf(numberOfMarks);
	for (int markNumber = 1; markNumber <= totalMarks; markNumber++) {
	    markNumberComboBox.addItem(Integer.toString(markNumber));
	}
	this.setVisible(true);
    }
}
